package com.dodo.videoEdit.ui.filechoseactivity;

import androidx.lifecycle.MutableLiveData;

import com.azhon.basic.lifecycle.BaseViewModel;
import com.dodo.proiectCore.util.LogUtil;
import com.dodo.videoEdit.MyApplication;
import com.dodo.videoEdit.annotation.SPKey;
import com.dodo.videoEdit.filetool.AudioProvider;
import com.dodo.videoEdit.filetool.ImageProvider;
import com.dodo.videoEdit.filetool.VideoProvider;
import com.dodo.videoEdit.greendao.Audio;
import com.dodo.videoEdit.greendao.AudioDao;
import com.dodo.videoEdit.greendao.FileDirType;
import com.dodo.videoEdit.greendao.FileDirTypeDao;
import com.dodo.videoEdit.greendao.Image;
import com.dodo.videoEdit.greendao.ImageDao;
import com.dodo.videoEdit.greendao.Video;
import com.dodo.videoEdit.greendao.VideoDao;
import com.dodo.videoEdit.utils.SPUtil;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
import com.xuexiang.rxutil2.rxjava.task.RxAsyncTask;
import com.xuexiang.rxutil2.rxjava.task.RxIOTask;

import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * @author zhoubo
 * @date 2020/8/12
 * @describe
 */
public class FileChoseViewModel extends BaseViewModel {

       private MutableLiveData<List<Audio>> audioList = new MutableLiveData<>();
       private MutableLiveData<List<Image>> imageList = new MutableLiveData<>();
       private MutableLiveData<List<Video>> videoList = new MutableLiveData<>();
       private MutableLiveData<List<FileDirType>> audioDir = new MutableLiveData<>();
       private MutableLiveData<List<FileDirType>> imageDir = new MutableLiveData<>();
       public MutableLiveData<List<FileDirType>> videoDir = new MutableLiveData<>();

    public void loadLocalFileToDateBase() {
        SPUtil.put(MyApplication.getApplication(), SPKey.UPLOAD_LOCAL_FILE_DATA,true);
        Disposable disposable = RxJavaUtils.doInIOThread(new RxAudioTask(this));
        Disposable disposable2 = RxJavaUtils.doInIOThread(new RxImageTask(this));
        Disposable disposable1 = RxJavaUtils.doInIOThread(new RxVideoTask(this));
        addDisposable(disposable);
        addDisposable(disposable2);
        addDisposable(disposable1);
    }

    public MutableLiveData<List<Audio>> getAudioList() {
        return audioList;
    }

    public MutableLiveData<List<Image>> getImageList() {
        return imageList;
    }

    public MutableLiveData<List<Video>> getVideoList() {
        return videoList;
    }

    public MutableLiveData<List<FileDirType>> getAudioDir() {
        return audioDir;
    }

    public MutableLiveData<List<FileDirType>> getImageDir() {
        return imageDir;
    }

    public MutableLiveData<List<FileDirType>> getVideoDir() {
        return videoDir;
    }

    public void isDateBaseLoad() {
        boolean flag = (Boolean) SPUtil.get(MyApplication.getAppInstance(), SPKey.UPLOAD_LOCAL_FILE_DATA, false);
        if (!flag) {
            loadLocalFileToDateBase();
        }
    }

    public void findVideoDir() {
        addDisposable(RxJavaUtils.executeAsyncTask(new RxAsyncTask<String, List<FileDirType>>("") {
            @Override
            public void doInUIThread(List<FileDirType> fileDirTypes) {
                LogUtil.d("数据量:"+fileDirTypes.size());
                videoDir.postValue(fileDirTypes);
            }

            @Override
            public List<FileDirType> doInIOThread(String s) {
                return MyApplication.getAppInstance().getDaoSession().getFileDirTypeDao().queryBuilder().where(FileDirTypeDao.Properties.IsVideoDir.eq(true)).build().list();
            }
        }));
    }

    public void findAudioDir() {
        addDisposable(RxJavaUtils.executeAsyncTask(new RxAsyncTask<String, List<FileDirType>>("") {
            @Override
            public void doInUIThread(List<FileDirType> fileDirTypes) {
                LogUtil.d("数据量:"+fileDirTypes.size());
                audioDir.postValue(fileDirTypes);
            }

            @Override
            public List<FileDirType> doInIOThread(String s) {
                return MyApplication.getAppInstance().getDaoSession().getFileDirTypeDao().queryBuilder().where(FileDirTypeDao.Properties.IsAudioDir.eq(true)).build().list();
            }
        }));
    }

    public void findImageDir() {
        addDisposable(RxJavaUtils.executeAsyncTask(new RxAsyncTask<String, List<FileDirType>>("") {
            @Override
            public void doInUIThread(List<FileDirType> fileDirTypes) {
                LogUtil.d("数据量:"+fileDirTypes.size());
                imageDir.postValue(fileDirTypes);
            }

            @Override
            public List<FileDirType> doInIOThread(String s) {
                return MyApplication.getAppInstance().getDaoSession().getFileDirTypeDao().queryBuilder().where(FileDirTypeDao.Properties.IsImageDir.eq(true)).build().list();
            }
        }));
    }


    public void findAudioListByDirPath(String path) {
        addDisposable(RxJavaUtils.executeAsyncTask(new RxAsyncTask<String, List<Audio>>(path) {
            @Override
            public void doInUIThread(List<Audio> fileDirTypes) {
                audioList.setValue(fileDirTypes);
            }

            @Override
            public List<Audio> doInIOThread(String s) {
                return MyApplication.getAppInstance().getDaoSession().getAudioDao().queryBuilder().where(AudioDao.Properties.ParentDir.eq(s)).build().list();
            }
        }));
    }

    public void findVideoListByDirPath(String path) {
        addDisposable(RxJavaUtils.executeAsyncTask(new RxAsyncTask<String, List<Video>>(path) {
            @Override
            public void doInUIThread(List<Video> fileDirTypes) {
                videoList.setValue(fileDirTypes);
            }

            @Override
            public List<Video> doInIOThread(String s) {
                return MyApplication.getAppInstance().getDaoSession().getVideoDao().queryBuilder().where(VideoDao.Properties.ParentDir.eq(s)).build().list();
            }
        }));
    }


    public void findImageListByDirPath(String path) {
        addDisposable(RxJavaUtils.executeAsyncTask(new RxAsyncTask<String, List<Image>>(path) {
            @Override
            public void doInUIThread(List<Image> fileDirTypes) {
                imageList.setValue(fileDirTypes);
            }

            @Override
            public List<Image> doInIOThread(String s) {
                return MyApplication.getAppInstance().getDaoSession().getImageDao().queryBuilder().where(ImageDao.Properties.ParentDir.eq(s)).build().list();
            }
        }));
    }



    public static class RxImageTask extends RxIOTask<FileChoseViewModel> {


        public RxImageTask(FileChoseViewModel inData) {
            super(inData);
        }

        @Override
        public Void doInIOThread(FileChoseViewModel s) {
            ImageProvider imageProvider = new ImageProvider(MyApplication.getAppInstance());
            imageProvider.getList();
            s.findImageDir();
            return null;
        }
    }

    public static class RxVideoTask extends RxIOTask<FileChoseViewModel> {

        private final VideoProvider videoProvider;

        public RxVideoTask(FileChoseViewModel inData) {
            super(inData);
            videoProvider = new VideoProvider(MyApplication.getAppInstance());
        }

        @Override
        public Void doInIOThread(FileChoseViewModel s) {
            videoProvider.getList();
            s.findVideoDir();
            return null;
        }
    }

    public static class RxAudioTask extends RxIOTask<FileChoseViewModel> {

        private final AudioProvider audioProvider;

        public RxAudioTask(FileChoseViewModel inData) {
            super(inData);
            audioProvider = new AudioProvider(MyApplication.getAppInstance());

        }

        @Override
        public Void doInIOThread(FileChoseViewModel s) {
            audioProvider.getList();
            s.findAudioDir();
            return null;
        }
    }

}
